/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lookvetinfo;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class LookServlet extends HttpServlet {

  
    public void wrong1(){    //对话框提示信息
        String msg="输入信息不能为空，登录失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    public void right(){    //对话框提示信息
        String msg="查询成功！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
     protected void doGet (HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        String vetname = new String(request.getParameter("vetname").getBytes("ISO-8859-1"),"UTF-8");
        String major=new String(request.getParameter("major").getBytes("ISO-8859-1"),"UTF-8");
        if((vetname.equals(""))&&(major.equals(""))){
             wrong1();
            response.sendRedirect("http://localhost:8084/jspsx/vet/vetinfor.jsp");
        }else if((!vetname.equals(""))&&(major.equals(""))){
             try{
                Connection con=null;
                Statement stmt=null;
                ResultSet rs=null;
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/abin? useUnicode=true&characterEncoding=gbk";
                con=DriverManager.getConnection(url,"root","admin");
                stmt=con.createStatement();
                String sql1="select * from t_vetinfo where vetname='"+vetname+"'";
                rs=stmt.executeQuery(sql1);
                //实例化保存个人信息的JavaBean
                LookVetinfoBean mm=new LookVetinfoBean();
                while(rs.next()){
                    mm.setVetname(rs.getString("vetname"));
                    mm.setMajor(rs.getString("major"));    
                        }
                            //获取session 对象
                            HttpSession session=request.getSession();
                            ArrayList vetlist =new ArrayList();  //实例化列表对象
                            vetlist.add(mm);                     //把个人信息保存到列表中
                             //把列表保存到session对象中，以便在别的页面中获取个人信息
                             session.setAttribute("vetlist",vetlist);
                             rs.close();
                             stmt.close();
                             con.close();
                             right();
                             response.sendRedirect("http://localhost:8084/jspsx/vet/lookvetinfor.jsp");   
            }catch(Exception e){
                e.printStackTrace();
            }
            
        }
        else if((vetname.equals(""))&&(!major.equals(""))){
            try{
                Connection con=null;
                Statement stmt=null;
                ResultSet rs=null;
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/abin? useUnicode=true&characterEncoding=gbk";
                con=DriverManager.getConnection(url,"root","admin");
                stmt=con.createStatement();
                String sql2="select * from t_vetinfo where major='"+major+"'";
                rs=stmt.executeQuery(sql2);
                //实例化保存个人信息的JavaBean
                LookVetinfoBean mm=new LookVetinfoBean();
                while(rs.next()){
                    mm.setVetname(rs.getString("vetname"));
                    mm.setMajor(rs.getString("major"));    
                        }
                            //获取session 对象
                            HttpSession session=request.getSession();
                            ArrayList vetlist =new ArrayList();  //实例化列表对象
                            vetlist.add(mm);                     //把个人信息保存到列表中
                             //把列表保存到session对象中，以便在别的页面中获取个人信息
                             session.setAttribute("vetlist",vetlist);
                             rs.close();
                             stmt.close();
                             con.close();
                             right();
                             response.sendRedirect("http://localhost:8084/jspsx/vet/lookvetinfor.jsp");
                      
            }catch(Exception e){
                e.printStackTrace();
            }
            
        }
     }
      protected void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        doGet(request,response);
    }
}
